package com.gin.kotlin.gf2gamecommunity.base.composable

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog

@Composable
fun IntSliderDialog(
    min: Int = 0,
    max: Int,
    step: Int = (max - min).coerceAtLeast(1) - 1,
    onDismissRequest: () -> Unit,
    onConfirmation: (count: Int) -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable (count: Int) -> Unit,
) {
    Dialog(onDismissRequest = onDismissRequest) {
        Card(
            shape = RoundedCornerShape(16.dp),
        ) {
            Column(
                modifier = modifier.padding(16.dp),
            ) {
                var sliderPosition by remember { mutableFloatStateOf(max.toFloat()) }

                content(sliderPosition.toInt())

                Slider(
                    value = sliderPosition,
                    onValueChange = { sliderPosition = (it + 0.5f).toInt().toFloat() },
                    steps = step,
                    valueRange = min.toFloat()..max.toFloat()
                )

                Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
                    TextButton(
                        onClick = { onDismissRequest() },
                        modifier = Modifier.padding(2.dp),
                    ) {
                        Text("取消")
                    }
                    TextButton(
                        onClick = { onConfirmation(sliderPosition.toInt()) },
                        modifier = Modifier.padding(2.dp),
                    ) {
                        Text("确认")
                    }
                }
            }
        }
    }
}